﻿using System;
using System.Diagnostics;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YJKRevitBase.WinForm;
using YJKRevitView;
using YJKPatternSign.StairsRoomPlanSign.ControlLayer;

namespace YJKPatternSign.StairsRoomPlanSign
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class CmdStairsRoomPlanSign : IExternalCommand
	{
		public static double NearTolerance
		{
			get
			{
				return CmdStairsRoomPlanSign.m_dNearTolerance;
			}
			private set
			{
				CmdStairsRoomPlanSign.m_dNearTolerance = value;
			}
		}

		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			if (CmdStairsRoomPlanSign._hWndRevit == null)
			{
				CmdStairsRoomPlanSign._hWndRevit = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			CmdViewType types = CmdViewType.FloorPlan;
			if (SwitchView.SetUsableView(types, commandData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			SignManger signManger = new SignManger(commandData);
			if (!signManger.TryGetStairPlanData())
			{
				return Autodesk.Revit.UI.Result.Succeeded;
			}
			if (!signManger.TryMakeSign())
			{
				YJKMessageBox.Show("标注失败");
			}
			else
			{
				commandData.Application.ActiveUIDocument.ActiveView = SignManger.ThisStairPlandDataPacker.UseView;
				SignManger.Uidoc.RefreshActiveView();
				YJKMessageBox.Show("标注完成");
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private static double m_dNearTolerance = 0.01;

		public static WindowHandle _hWndRevit = null;
	}
}
